<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 角色组ajax类
 * @package  ajax
 * @subpackage  backend
 * @author   create by allen.qiang
 */

quanli_require('module/base/system/sysRole.class.php');

class ajax_roleGroupList {

	public function execute() {

        if (isset($_REQUEST['operFlg']) && is_numeric($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch ($_REQUEST['operFlg']) {

                case 1: //角色组列表查询
                    $this->searchRoleGourpList();
                    break;

                case 2: //角色组详细查询
                    $this->searchRoleGourpDetail();
                    break;

                case 100: //编辑权限

                    if (!isset($_REQUEST['act']) || !in_array($_REQUEST['act'], array('del', 'edit'))) {
                        returnJsonData('参数错误');
                    }
                    if ($_REQUEST['act'] == 'edit') {

                        $this->editRoleGroup();

                    } else if ($_REQUEST['act'] == 'del') {

                        $this->deleteRoleGroup();

                    }

                default:
                    returnJsonData('参数错误');
                    break;

            }

        } else {

            returnJsonData('参数错误');

        }

	}

    /**
     * 角色组详细查询
     */
    private function searchRoleGourpDetail() {

        if (!isset($_REQUEST['id']) || !quanli_is_int($_REQUEST['id'])) {
            returnJsonData('参数错误');
        }

        //实例角色类
        $sysRole = new sysRole();

        //查询角色组信息
        $arg_array = array(
            'roleGroupID' => $_REQUEST['id']
        );
        $role_group_array = $sysRole->searchRoleGroup( $arg_array );

        if ($role_group_array) {
            returnJsonData('ok', 200, $role_group_array[0]);
        } else {
            returnJsonData('角色组信息没有找到', 404);
        }

    }

    /**
     * 角色组列表
     */
    private function searchRoleGourpList() {
		
		global $G_PGLOB;
		
		//实例角色类
		$sysRole = new sysRole();
		
		//查询角色组信息
        $arg_array = $_REQUEST;
		$role_group_array = $sysRole->searchRoleGroup( $arg_array );

        //组织返回的数据
        $result_array = array(
            'page' => $arg_array['page'],
            'total' => $arg_array['count'],
            'rows' => array()
        );

		if ($role_group_array) {

            $edit_permission = $G_PGLOB->has_permission(3);

			foreach ($role_group_array as $role_group) {

                if ($edit_permission) {
                    $result_array['rows'][] = array (
                        'id' => $role_group['roleGroupID'],
                        'cell' => array(
                            '<a href="javascript:void(0);" onclick="roleGroup.edit.open(' . $role_group['roleGroupID'] . ');">修改</a>',
                            $role_group['roleGroupID'],
                            $role_group['roleGroupName'],
                            $role_group['memo']
                        )
                    );
                } else {
                    $result_array['rows'][] = array (
                        'id' => $role_group['roleGroupID'],
                        'cell' => array(
                            $role_group['roleGroupID'],
                            $role_group['roleGroupName'],
                            $role_group['memo']
                        )
                    );
                }


			}
		}

		echo json_encode($result_array);

	}
	
	private function editRoleGroup() {

		global $G_PGLOB;

        //实例角色类
        $sysRole = new sysRole();
		
		//新增或修改角色组
        $arg_array = $_REQUEST;
        $arg_array['operator'] = $G_PGLOB->currentUserID;
		$result_array = $sysRole->editRoleGroup( $arg_array );

        returnJsonData($result_array['message'], $result_array['code']);

	}
	
	private function deleteRoleGroup () {

        if (!isset($_REQUEST['r_g_array']) || empty($_REQUEST['r_g_array'])) {
            $this->toReturn('请选择要删除的角色组');
        }

		global $G_PGLOB;

        //实例角色类
        $sysRole = new sysRole();

        //批量删除角色组信息
		$result_array = $sysRole->deleteRoleGroup( explode(',', $_REQUEST['r_g_array']) );

        returnJsonData($result_array['message'], $result_array['code']);
	
	}
}